package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import play.db.jpa.Model;
import play.libs.Codec;
import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;

/**
 * User entity managed
 */
@Entity
@Table(name="xtd_user")
public class User extends Model {
	   
    public String name;
    
    public String realName;

    public String password;
    
    public String email;
    
    public String tel; 
    
    public String sex;

    public String job;  //专业技术职务
  
    public Date birthday;	//出生
    
    public Date jobInTime; //入职时间
    
    public Date joinPartyTime;//入党时间
    
    public String politics;
    
    public String nation;//民族
    
    public String address; //住址
    
    public String orderNum;  //排序号

    public Date   lastLoginTime;

	@ManyToOne
    public Dept dept;
    
    @Transient
    public long deptId;
    
    @Transient
    public String deptName;
    
    @Transient
    public String roleStr;
    
    @Transient
    public String roleId;
  
    public long getDeptId(){
    	return dept==null?0:dept.id;
    }
    public String getDeptName(){
    	return dept==null?"":dept.name;
    }

    public String toString() {
        return "User(" + email + ")";
    }
    
    public void initPassword() {
       // if (this.password == null) this.password = Codec.hexMD5("ydda");
        if (this.password == null) this.password = Codec.hexMD5("123456");
    }
    
    public void initRole(Long[] roles){
    	if(roles==null){
    		return;
    	}
    	UserRole.delete("user=?", this);
    	for(Long roleId:roles){
    		UserRole  ur = new UserRole();
    		ur.role = Role.findById(roleId);
    		ur.user = this;
    		ur.save();
    	}
    }
    
    public String getRoleStr(){
    	String roleStr="";
    	List<UserRole> urs = UserRole.findByUser(this);
    	for(UserRole ur: urs){
    		roleStr+=ur.role.name+"<br/>";
    	}
    	return roleStr.replaceAll("<br/>$", "");
    }
    
    public String getRoleId(){
    	String roleId="";
    	List<UserRole> urs = UserRole.findByUser(this);
    	for(UserRole ur: urs){
    		roleId+=ur.role.id+",";
    	}
    	roleId+="";
    	return roleId.replaceAll(",$", "");
    }

    public boolean checkPassword(String password) {
        return this.password.equals(Codec.hexMD5(password));
    }
    	
    public static User findByName(String name) {
        return find("name", name).first();
    }
    
    public void changePassWord(String password) {
		this.password = Codec.hexMD5(password);
	}

    public static String toCheckJson(List<User> model) {
    	return new JSONSerializer()
    	  .transform(new DateTransformer("yyyy-MM-dd"), "birthday")
          .transform(new DateTransformer("yyyy-MM-dd"), "jobInTime")
          .transform(new DateTransformer("yyyy-MM-dd"), "joinPartyTime")
    	  .include("id", "name", "realName", "email", "lastLoginTime", "tel", "sex","roleStr","roleId",
    			  "address","nation","birthday", "politics","deptId","deptName",
    			  "jobInTime","joinPartyTime", "orderNum")
    	  .exclude("*")
    	  .serialize(model);
    }
    
    public List<String> roleIdList(){
    	List<String> roleIds = new ArrayList<String>();
    	List<UserRole> urs = UserRole.find("user", this).fetch();
    	for(UserRole ur:urs){
    		roleIds.add(ur.role.id+"");
    	}
    	return roleIds;
    }
    

}